<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>

<?php


// define variables and set to empty values
$nameErr = $emailErr = $rollNumberErr = $confirmPasswordErr = $passwordErr = $genderErr = $websiteErr = $passErr = $RegistrationErr = $collegeErr = "";
$name = $email = $rollNumber = $password = $confirmPassword = $gender = $pass = $college = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$con=mysqli_connect("localhost","root","","smartinterview");
         // Check connection
            if (mysqli_connect_errno())
             {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
             }
		
		
   if (empty($_POST["name"]))
     {$nameErr = "Name is required";}
   else
     {
     $name = test_input($_POST["name"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$name))
       {
       $nameErr = "Only letters and white space allowed";
       }
     }
	 
	 if (empty($_POST["college"]))
     {$collegeErr = "College/University Name is required";}
   else
     {
     $college = test_input($_POST["college"]);
     // check if name only contains letters and whitespace
     if (!preg_match("/^[a-zA-Z ]*$/",$college))
       {
       $nameErr = "Only letters and white space allowed";
       }
     }
  
   if (empty($_POST["email"]))
     {$emailErr = "Email is required";}
   else
     {
     $email = test_input($_POST["email"]);
     // check if e-mail address syntax is valid
     if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
       {
       $emailErr = "Invalid email format";
       }
     }
    	$result = mysqli_query($con,"SELECT UserEmail FROM login_details");
			 while($row = mysqli_fetch_array($result))
				{	
				if($row['UserEmail'] == $email)
					{
				$RegistrationErr = "Email is already registered.Try Again";
					}
					}
if (empty($_POST["rollNumber"]))
     {$rollNumberErr = "RollNumber is required";}
   else
     {
     $rollNumber = test_input($_POST["rollNumber"]);
     // check if e-mail address syntax is valid
     }
if (empty($_POST["password"]))
     {$passwordErr = "Password is required";}
else if (empty($_POST["confirmPassword"]))
     {$passwordErr = "Confirm Password is required";}
   
     if(!empty($_POST["password"]) && !empty($_POST["confirmPassword"])){
     $password = test_input($_POST["password"]);
	 $confirmPassword = test_input($_POST["confirmPassword"]);
     // check if both passwords match
	 if($password != $confirmPassword)
    {
	$passwordErr = "Passwords did not match";
     }
	 else 
	 {
	 $pass = $password;
	 }
	 }


   if (empty($_POST["gender"]))
     {$genderErr = "Gender is required";}
   else
     {$gender = test_input($_POST["gender"]);}
}    $isadded="no";
     if($name!= "" && $email != "" && $rollNumber != "" && $gender != "" && $pass != "" && $college != "" && $RegistrationErr == "" && $rollNumberErr == ""){
	
	 $username = addslashes(mysql_real_escape_string($name)); 
     $useremail = addslashes(mysql_real_escape_string($email));
	 $userrollno = addslashes(mysql_real_escape_string($rollNumber));
     $usergender = addslashes(mysql_real_escape_string($gender));
     $userpassword = md5(addslashes(mysql_real_escape_string($pass)));
	 $usercollege = addslashes(mysql_real_escape_string($college));
     $userrole = 2;	 

	  $sql="INSERT INTO login_details (UserName,UserEmail,UserCollege,UserRollNo,UserGender,UserPassword,UserRole)
         VALUES
         ('$username','$useremail','$usercollege','$userrollno','$usergender','$userpassword','$userrole')";
		   if (!mysqli_query($con,$sql))
           {
           die('Error: ' . mysqli_error($con));
           }
	  $isadded="yes";
	  $url="loginpage.php?isadded=$isadded";						// to redirecting the server in location.php
					header("Refresh:0;URL=$url");
	  exit(1);
	 }
 
 
 
 
function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>
<form method="post" action=" ">

<h2>Online&nbspTest&nbspRegistration&nbspForm</h2>

   Name: </td><td><input type="text" name="name" value="<?php echo $name;?>">
 <span class="error"> <?php echo $nameErr;?></span>
   <br><br>

   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error"> <?php echo $emailErr;?></span>
<span class="error"> <?php echo $RegistrationErr;?></span>
   <br><br>
   Roll Number: <input type="text" name="rollNumber" placeholder="Eg:DE/11/EC/002"value="<?php echo $rollNumber;?>">
    <span class="error"> <?php echo $rollNumberErr;?></span>
   <br><br>
    University/College:</td><td> <input type="text" name="college" value="<?php echo $college;?>">
   <span class="error"> <?php echo $collegeErr;?></span>
   <br><br>
   <span class="error"> <?php echo $passErr;?></span>
   Password:</td><td> <input type="password" name="password" value="<?php echo $password;?>">
   <span class="error"> <?php echo $passwordErr;?></span>
   <br><br>
    Confirm Password: <input type="password" name="confirmPassword" value="<?php echo $confirmPassword;?>">
   <span class="error"> <?php echo $confirmPasswordErr;?></span>
   <br><br>
   Gender:
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?>  value="female">Female
   <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?>  value="male">Male
   <td><span class="error"> <?php echo $genderErr;?></span></td></tr>
   <br><br>
   <input type="submit" name="submit" value="Submit">
   <a href="loginpage.php?isAdmin=yes">Super Admin Login</a> 
</form>
</body>
</html>